package com.basis.handler;

import org.apache.log4j.Logger;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.hibernate.SQLQuery;
import org.openbravo.client.kernel.BaseActionHandler;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public class LoadImportButtonHandler extends BaseActionHandler {
	private Logger logger = Logger.getLogger(this.getClass());

	@Override
	protected JSONObject execute(Map<String, Object> parameters, String data) {
		JSONObject result = new JSONObject();
		try {
			OBContext.setAdminMode();
			String sql = "select code, name, ad_Tab_Id, template_path, hide_imp_btn " +
					" from Emes_Data_Import where ad_tab_id is not null";
			SQLQuery query = OBDal.getInstance().getSession().createSQLQuery(sql);
			List<Object[]> list = query.list();

			JSONArray array = new JSONArray();
			JSONObject row = null;
			for (Object[] arr : list) {
				row = new JSONObject();
				row.put("code", arr[0]);
				row.put("name", arr[1]);
				row.put("tabId", arr[2]);
				row.put("templatePath", arr[3]);
				row.put("hideImpBtn", arr[4]);
				
				array.put(row);
			}

			result.put("list", array);
			result.put("success", true);
			OBDal.getInstance().commitAndClose();
		} catch (Exception e) {
			OBDal.getInstance().rollbackAndClose();
			logger.error("数据异常：" + e.getMessage(), e);
			try {
				result.put("result", false);
				result.put("msg", "数据异常");
			} catch (Exception e1) {
			}
		} finally {
			OBContext.restorePreviousMode();
		}
		return result;
	}
}
